package elitech.vietnam.weatherfashion.dialog;

import java.io.IOException;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import elitech.vietnam.weatherfashion.EditPostActivity;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;

public class EditPostDialog extends Dialog implements View.OnClickListener {

	private PostDTO post;
	private Button button_edit, button_delete;
	MainActivity activity;
	int memId;

	public EditPostDialog(Context context, PostDTO post, int memId) {
		super(context, R.style.Dialog);
		this.activity = (MainActivity) context;
		this.post = post;
		this.memId = memId;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_editpost);

		button_edit = (Button) findViewById(R.id.dialog_buttonedit);
		button_delete = (Button) findViewById(R.id.dialog_buttondelete);

		button_edit.setOnClickListener(this);
		button_delete.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dialog_buttonedit:
			Intent intent = new Intent(activity, EditPostActivity.class);
			intent.putExtra("PostID", post.getId());
			intent.putExtra("PostContent", post.getContent());
			intent.putExtra("PostImage", post.getImage());
			intent.putExtra("MemID", memId);
			activity.startActivityForResult(intent, MainActivity.REQ_CODE_EDITPOST);
			dismiss();
			break;

		case R.id.dialog_buttondelete:
			dismiss();
			new ConfirmDialog.Builder(activity)
			.setMessage(activity.getString(R.string.do_you_want_to_delete_this_post))
			.setYesClick(new ConfirmDialog.OnYesClick() {
				@Override
				public void doClick() {
					new DeletePostTask().execute();
				}
			}).build().show();
			break;

		default:
			break;
		}
	}

	class DeletePostTask extends AsyncTask<Integer, Integer, Boolean> {
		MyProgressDialog dialog;

		@Override
		protected Boolean doInBackground(Integer... params) {
			PHPSoapServiceCaller service = new PHPSoapServiceCaller();
			try {
				return service.deletePostMember(post.getStoreId(), post.getId());
			} catch (IOException e) {
				return false;
			}
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new MyProgressDialog(activity);
			dialog.setCancelable(false);
			dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			dialog.dismiss();
			if (result) {
				activity.onBackPressed();
				activity.getIntent().putExtra("SELF_RELOAD", 1);
				WFApplication.getInstance().stylerAllData = null;
				Toast.makeText(activity, getContext().getString(R.string.delete_successful), Toast.LENGTH_LONG).show();
			} else
				Toast.makeText(activity, getContext().getString(R.string.delete_failed), Toast.LENGTH_LONG).show();
		}
	}
}
